﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using ScrumTable.Common.Collections;
using ScrumTable.Common.Monitoring;
using ScrumTable.Common.Security;
using ScrumTable.DL.Data.Common;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.Generic.Test.Mocks;
using ScrumTable.DL.Data.Generic.Test.Mocks.Config.Driver;
using ScrumTable.DL.Data.Generic.Test.Mocks.Config.Schema;

#endregion

namespace ScrumTable.DL.Data.Generic.Test.Cache
{
    /// <summary>
    /// This is the test class for a ICacheElement class.
    /// </summary>
    [TestClass]
    public abstract class CacheIdentityBaseTest<T>
        where T : ICacheIdentity
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the data context with the data management facilities.
        /// </summary>
        protected GenericDataContextStub DataContext { get; private set; }

        /// <summary>
        /// Gets the cache context with the cache management facilities.
        /// </summary>
        protected CacheContext CacheContext { get; private set; }

        /// <summary>
        /// Gets the element to test.
        /// </summary>
        protected T ToTest { get; set; }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        /// <summary>
        /// Gets the resources associated to the current test.
        /// </summary>
        protected ResourceManager Resources { get; private set; }

        #endregion

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new CacheIdentityBaseTest instance.
        /// </summary>
        public CacheIdentityBaseTest()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Use TestInitialize to run code before running each test .
        /// </summary>
        public virtual void TestInitialize()
        {
            DataLoaderMock dataLoader = new DataLoaderMock();
            DataContext = new GenericDataContextStub(
                dataLoader,
                new DataSaverStub(CacheSavePairState.Saved),
                new DataContextInfoStub(
                    TestContext.TestDir,
                    dci => new GenericDataSchemaSelectorStub(dci),
                    dci => new GenericDriverConfigSelectorStub(dci)));
            dataLoader.Schema = DataContext.Schema;
            DataContext.Connection.Connect(
                new ConnectionInformationManager(ConnectionInformation.Empty),
                result => { },
                null );
            DataContext.ImportData(
                new ValueChain<string>(DataContext.DataCache.Projects.First().UniqueId),
                new NullDataImportMonitor());

            CacheContext = DataContext.DataCache;
            Resources = dataLoader.Resources;
        }

        /// <summary>
        /// Use TestCleanup to run code after each test has run.
        /// </summary>
        public virtual void TestCleanup()
        {
            DataContext.Dispose();
            DataContext = null;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
